# coding:utf-8
import os
import sys

from PySide6.QtCore import QTranslator
from PySide6.QtGui import QGuiApplication, Qt
from PySide6.QtWidgets import QApplication
from qfluentwidgets import FluentTranslator, setTheme, Theme

from app.common.config import cfg
from app.utils.log_rec import logger
from app.view.MainWindow import MainWindow
from app.resources import resources_rc


# def load_theme():
#     # 根据配置加载主题
#     theme = cfg.get(cfg.theme)
#     logger.debug(theme)
#     if theme.upper() == "light":
#         setTheme(Theme.LIGHT)
#     elif theme.upper() == "dark":
#         setTheme(Theme.DARK)
#     else:
#         setTheme(Theme.AUTO)


# 启用 DPI 缩放
logger.debug("启用 DPI 缩放")
if cfg.get(cfg.dpiScale) == "Auto":
    QGuiApplication.setHighDpiScaleFactorRoundingPolicy(
        Qt.HighDpiScaleFactorRoundingPolicy.Ceil
    )
else:
    os.environ["QT_ENABLE_HIGHDPI_SCALING"] = "0"
    os.environ["QT_SCALE_FACTOR"] = str(cfg.get(cfg.dpiScale))

# 创建应用程序
logger.debug("创建应用程序")
app = QApplication(sys.argv)
app.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.Ceil)

# 国际化
logger.debug("加载国际化")
locale = cfg.get(cfg.language).value
translator = FluentTranslator(locale)
testToolsTranslator = QTranslator()
testToolsTranslator.load(locale, "translator", ".", ":/qm/i18n/")

app.installTranslator(translator)
app.installTranslator(testToolsTranslator)
# load_theme()
# 创建主窗口
logger.debug("创建主窗口")
w = MainWindow()
w.show()

app.exec()
